package com.task.mall.controller.admin;

import com.google.gson.Gson;
import com.task.mall.bean.Admin;
import com.task.mall.bean.Login;
import com.task.mall.bean.Password;
import com.task.mall.bean.Result;
import com.task.mall.service.AdminService;
import com.task.mall.service.impl.AdminServiceImpl;
import com.task.mall.utils.HttpUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/api/admin/admin/*")
public class AdminServlet extends HttpServlet {
    private AdminService adminService = new AdminServiceImpl();
    Gson gson = new Gson();
    /**
     * 登录、新增的时候是post请求
     * /api/admin/admin/login  登录
     * /api/admin/admin/addAdmin  新增
     * /api/admin/admin/updateAdminss 修改
     * /api/admin/admin/getSearchAdmins 模糊查询
     * /api/admin/admin/changePwd 更改密码
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/admin/", "");
        if("login".equals(action)){
            login(request,response);
        }else if ("addAdminss".equals(action)){
            addAdminss(request,response);
        }else if ("getSearchAdmins".equals(action)){
            getSearchAdmins(request,response);
        }else if ("updateAdminss".equals(action)){
            updateAdminss(request,response);
        }else if ("changePwd".equals(action)){
            changePwd(request,response);
        }else if ("logoutAdmin".equals(action)){
            logoutAdmin(request,response);
        }
    }

    /**
     * 退出管理员账户
     * @param request
     * @param response
     */
    private void logoutAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        // 下面这句好像没有用到
        Login login = gson.fromJson(requestBody, Login.class);
        Result res = new Result();
        res.setCode(0);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 更改管理员密码 -- 需要提供旧密码
     */
    private void changePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Password password = gson.fromJson(requestBody, Password.class);
        Result res = new Result();
        int result = adminService.changePwd(password);
        if (result == 200){
            res.setCode(0);
        } else {
            res.setCode(10000);
            if (result == 201) {
                res.setMessage("旧密码不正确");
            } else if (result == 202){
                res.setMessage("两次输入的新密码不一致，请确认后再次输入");
            } else{
                res.setMessage("当前访问异常，稍后重试");
            }
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 修改管理员基本信息
     */
    private void updateAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 输入流，输出流
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        int result = adminService.updateAdminss(admin);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
        } else {
            res.setCode(10000);
            res.setMessage("当前访问异常，稍后重试");
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 新增管理员业务逻辑
     */
    private void addAdminss(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 输入流，输出流
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        //调用service层
        int result = adminService.addAdminss(admin);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
            res.setData("");
        }else if (result == 404){
            res.setCode(10000);
            res.setMessage("用户名或者密码正則表達式未通過");
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 登录的具体业务逻辑实现
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 输入流，输出流
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        //调用service层
        int result = adminService.login(admin);
        Result res = new Result();
        if (result == 200){
            request.getSession().setAttribute("email", admin.getEmail());
            Login loginInfo  = adminService.getLoginInfo(admin.getEmail());
            res.setCode(0);
            Map<String, Object> map = new HashMap<>();
            map.put("token",loginInfo.getNickName());
            map.put("name",loginInfo.getNickName());
            res.setData(map);
        }else if (result == 404){
            res.setCode(10000);
            res.setMessage("用户名或者密码错误");
        }else{
            res.setCode(10000);
            res.setMessage("当前访问异常，稍后重试");
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 根据条件(模糊查询)获取指定的admin管理员信息
     * 思路：1.获取请求体中的数据json ---> java对象 2.将java对象传入sql中 3.封装结果，返回
     */
    private void getSearchAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        Admin admin = gson.fromJson(requestBody, Admin.class);
        List<Admin> adminList = adminService.querySearchAdmins(admin);
        Result res = new Result();
        res.setCode(0);
        res.setData(adminList);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 查询、删除使用的是GET请求
     * /api/admin/admin/getAdminsInfo 获取管理员信息
     * /api/admin/admin/deleteAdmins 删除
     * /api/admin/admin/allAdmins 查询全部
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/admin/", "");
        if ("allAdmins".equals(action)){
            allAdmins(request,response);
        }else if ("deleteAdmins".equals(action)){
            deleteAdmins(request,response);
        }else if ("getAdminsInfo".equals(action)){
            getAdminsInfo(request,response);
        }
    }

    /**
     * 获取管理员email，nickname，pwd信息
     */
    private void getAdminsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Admin admin = adminService.getAdminsInfo(id);
        Result res = new Result();
        //获取到指定id的管理员信息界面
        res.setCode(0);
        res.setData(admin);
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 删除指定的管理员
     */
    private void deleteAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int result = adminService.deleteAdmins(id);
        Result res = new Result();
        if (result == 200){
            res.setCode(0);
            res.setData("");
        }else {
            res.setCode(10000);
            res.setMessage("当前访问异常，稍后重试");
        }
        response.getWriter().println(gson.toJson(res));
    }

    /**
     * 查询全部的admin管理员信息
     */
    private void allAdmins(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Admin> adminList = adminService.queryAllAdmins();
        Result res = new Result();
        res.setCode(0);
        res.setData(adminList);
        response.getWriter().println(gson.toJson(res));
    }
}
